package com.cszy.yydqbfq.ui.fragment

import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.cszy.yydqbfq.R
import com.cszy.yydqbfq.databinding.FragmentJingXuanBinding
import com.cszy.yydqbfq.adapter.IndexListAdapter
import com.cszy.yydqbfq.adapter.IndexListJingXuanAdapter
import com.cszy.yydqbfq.base.BaseVMFragment
import com.cszy.yydqbfq.bean.Song
import com.cszy.yydqbfq.common.PlaylistRepository
import com.cszy.yydqbfq.dialog.BottomPopupWithGeDanRecycler
import com.cszy.yydqbfq.ui.activity.PlayDetailActivity
import com.cszy.yydqbfq.viewmodel.IndexViewModel
import com.hlyj.http.ad.NewGroMoreFeedAdManager
import com.hlyj.http.base.tool.lib_hlyj_base.bean.music.MusicListBean

import com.hnyyac.ad.mds.utils.BaseAdConstants

class JingXuanFragment : BaseVMFragment<IndexViewModel, FragmentJingXuanBinding>() {
    private var listAdapter: IndexListAdapter? = null
    private var newGromoreFeedAd : NewGroMoreFeedAdManager? =null
    override fun initView(savedInstanceState: Bundle?) {

    }

    override fun initData() {
        mViewModel.queryMusic()
        listAdapter= IndexListAdapter()
        mBinding.recyMusicList.layoutManager=  LinearLayoutManager(requireActivity())
        mBinding.recyMusicList.adapter=listAdapter

        newGromoreFeedAd = NewGroMoreFeedAdManager(requireActivity(), BaseAdConstants.FEEDAD_Id, mBinding?.frameLayoutBanner,22)
        newGromoreFeedAd?.loadAd()
    }

    override fun initObserve() {
        //获取音频数据
        mViewModel.queryMusicResult.observe(this){musiclist->
            listAdapter?.submitList(musiclist as List<MusicListBean>?)

        }
    }

    override fun initListener() {
        listAdapter?.setOnItemClickListener { adapter, view, position ->
            // 传递整个列表和当前点击位置
            var songList=mViewModel.convertToSongInfoList(adapter.items)
            val intent = Intent(requireActivity(), PlayDetailActivity::class.java).apply {
                putParcelableArrayListExtra("MUSIC_LIST", ArrayList(songList))
                putExtra("CURRENT_POSITION", position)
            }
            startActivity(intent)
        }

        // 需要传递控件 id加入歌单点击事件
        listAdapter?.addOnItemChildClickListener(R.id.iv_detail) { adapter, view, position ->
            // toast("点了添加歌单")
            var song_item=adapter.items.get(position)
            // 将 MusicListBean 转换为 Song
            val song = Song().apply {
                title = song_item.name ?: ""
                artist = song_item.author ?: ""
                cover = song_item.imageUrl ?: ""
                uri=song_item.songUrl
                duration = 0
                songlistbean_id = 0
            }
            val repository = PlaylistRepository()
            var geDanList = repository.getAllPlaylists()
                .filter { it.name != "本地音乐" }  // 过滤条件
            BottomPopupWithGeDanRecycler(requireActivity(), geDanList,song).show(mBinding.root)
        }
    }

}